
import com.facebook.graph.FacebookMobile;

import flash.geom.Rectangle;
import flash.media.StageWebView;

import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

private var appID:String = "135214913299944";
public var xmlStorage:XMLStorage;
public var facebookInfo:FacebookInfo;

public var facebookMobile:FacebookMobile;

private function initProcess(event:FlexEvent):void
{
	
	
	//facebookMobile = new FacebookMobile();
	FacebookMobile.init(appID, handleInit);
	
	// TODO Auto-generated method stub
	if(getPref()){
		passLogin();
	}else{
		goLogin();
	}
	
	
}

private function handleInit(session:Object, fail:Object):void{
	
}

private function getPref():Boolean{
	
	xmlStorage = new XMLStorage();
	facebookInfo = xmlStorage.getFacebookInfo();
	
	if(facebookInfo != null && facebookInfo.accessToken != null){
		if(facebookInfo.accessToken != ''){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
	
	return true;
}

private function passLogin():void{
	
	view_loginView.visible = false;
	this.currentState = "MAIN";
	
	actCheckRegistration(facebookInfo.facebook_uid)
}

private var loginWebView:StageWebView = new StageWebView();

private function goLogin():void{
	
	//this.currentState = "LOGIN";
	view_loginView.visible = true;
}

public function openLoginWebView():void{
	
	loginWebView.stage = this.stage;
	loginWebView.viewPort = new Rectangle(0, 0, this.width, this.height);
	
	FacebookMobile.login(handleLogin, this.stage, ['email','user_birthday','user_about_me','user_status','user_work_history', 'user_activities', 'publish_actions', 'read_friendlists', 'status_update','friends_about_me', 'publish_stream','user_photos','user_videos','offline_access', 'read_stream','friends_activities','user_likes','user_location','friends_location'], loginWebView);
	
}

private function handleLogin(session:Object, fail:Object):void {
	if (session != null) {
		//
		trace("got uid : " + session.uid);
		
		saveFacebookSession(session);
		
	}else if(fail != null) {
		trace("failed : " + fail.toString());
	}
}

private function saveFacebookSession(session:Object):void{
	facebookInfo = new FacebookInfo();
	facebookInfo.setBySession(session);
	xmlStorage.saveByFacebookInfo(facebookInfo);
	
	passLogin();
}

private function actCheckRegistration(facebook_uid:String):void{
	httpserv_checkRegistration.url = "http://basetheory.org/app_api/" + "checkRegistration.php";
	
	var params:Object = new Object();
	params.fid = facebook_uid;
	
	httpserv_checkRegistration.send(params);
	
}


protected function resultCheckRegistration(event:ResultEvent):void
{
	// TODO Auto-generated method stub
	if(event.result.toString() == 'true'){
		passRegistration();
	}else{
		registration();
	}
	
}

protected function faultCheckRegistration(event:FaultEvent):void
{
	// TODO Auto-generated method stub
	
}

private function passRegistration():void{
	this.currentState = "MAIN";
}

private function registration():void{
	// insert registration processes.
	httpserv_registration.url = "http://basetheory.org/app_api/" + "registration.php";
	
	var params:Object = new Object();
	params.fid = facebookInfo.facebook_uid;
	
	httpserv_registration.send(params);
}


protected function resultRegistration(event:ResultEvent):void
{
	// TODO Auto-generated method stub
	
	
	passRegistration();
}

protected function faultRegistration(event:FaultEvent):void
{
	// TODO Auto-generated method stub
	trace("registration was failed");
}





